package com.redsoil.house.ui.matter.presenter;

import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RxPresenter;
import com.redsoil.house.model.DataManager;
import com.redsoil.house.model.bean.MessageListVO;
import com.redsoil.house.model.bean.OnePropertyVO;
import com.redsoil.house.model.http.exception.ApiException;
import com.redsoil.house.model.http.response.HttpResponse;
import com.redsoil.house.ui.login.contract.LoginContract;
import com.redsoil.house.ui.matter.contract.MatterContract;
import com.redsoil.house.utils.RxUtil;
import com.redsoil.house.widget.CommonSubscriber;

import javax.inject.Inject;

/**
 * 作者：weidingqiang on 2017/9/7 15:39
 * 邮箱：dqwei@iflytek.com
 */

public class MatterPresenter extends RxPresenter<MatterContract.View> implements MatterContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public MatterPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void attachView(MatterContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    private void registerEvent(){

    }

    @Override
    public void getMessageList(String keywords) {
        addSubscribe(mDataManager.getMessageList(AppContext.getInstance().getToken(),keywords)
                .compose(RxUtil.<HttpResponse<MessageListVO>>rxSchedulerHelper())
                .compose(RxUtil.<MessageListVO>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<MessageListVO>(mView) {
                            @Override
                            public void onNext(MessageListVO data) {

                                mView.getMessageListSuccess(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }

    @Override
    public void importance(String ids, String state) {
        addSubscribe(mDataManager.importance(AppContext.getInstance().getToken(),ids,state)
                .compose(RxUtil.<HttpResponse<String>>rxSchedulerHelper())
                .compose(RxUtil.<String>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<String>(mView) {
                            @Override
                            public void onNext(String data) {

                                mView.importanceSuccess();
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }

    @Override
    public void lock(String ids, String state) {
        addSubscribe(mDataManager.lock(AppContext.getInstance().getToken(),ids,state)
                .compose(RxUtil.<HttpResponse<String>>rxSchedulerHelper())
                .compose(RxUtil.<String>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<String>(mView) {
                            @Override
                            public void onNext(String data) {

                                mView.lockSuccess();
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }

    @Override
    public void delete(String ids) {
        addSubscribe(mDataManager.delete(AppContext.getInstance().getToken(),ids)
                .compose(RxUtil.<HttpResponse<String>>rxSchedulerHelper())
                .compose(RxUtil.<String>handleTestResult())
                .subscribeWith(
                        new CommonSubscriber<String>(mView) {
                            @Override
                            public void onNext(String data) {

                                mView.deleteSuccess();
                            }

                            @Override
                            public void onError(Throwable e) {
                                //当数据返回为null时 做特殊处理
                                try {
                                    int code = ((ApiException) e).getCode();
                                    mView.responeError(e.getMessage());
                                    return;
                                } catch (Exception ex) {

                                }
                                mView.responeError("数据请求失败，请检查网络！");
                            }

                        }
                )
        );
    }
}
